/*
 * PGN_Reader : A simple pgn reader for chess games.
 * Copyright (C) 2011 Laurent BERNABE
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gmail.bernabe.laurent.java_swing.pgn_reader.gui;

import com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.ChessPiece;

public class PieceToAnimate {
	
	public PieceToAnimate(ChessPiece pieceValue, BoardJPanel ownerBoardJPanel){
		if (pieceValue == null)
			throw new IllegalArgumentException("pieceValue is null !!!");
		this.pieceValue = pieceValue;
		this.ownerBoardJPanel = ownerBoardJPanel;
	}
	
	public void setStartPosition(BoardCell startCell){
		this.startCell = startCell;
		currentX = BoardJPanel.getPieceAbsolutePosition(startCell.getFile());
		currentY = BoardJPanel.getPieceAbsolutePosition(7-startCell.getRank());
	}
	
	public void setEndPosition(BoardCell endCell){
		finalX = BoardJPanel.getPieceAbsolutePosition(endCell.getFile());
		finalY = BoardJPanel.getPieceAbsolutePosition(7-endCell.getRank());
	}
	
	public void animate(){
		final int CYCLES_NUMBER = 50;
		final int regularDeltaX = (finalX - currentX) / CYCLES_NUMBER;
		final int regularDeltaY = (finalY - currentY) / CYCLES_NUMBER;
		animatorThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				boolean xPositionIsReached = false, yPositionIsReached = false;
				
				
				while (! xPositionIsReached  || ! yPositionIsReached){
					if (regularDeltaX != 0)
						xPositionIsReached = ( (finalX - currentX) / regularDeltaX ) <= 1;

					else
						xPositionIsReached = true;
					if (regularDeltaY != 0) 
						yPositionIsReached= ( (finalY - currentY) / regularDeltaY ) <= 1;
					else
						yPositionIsReached = true;

					currentX += regularDeltaX;
					currentY += regularDeltaY;
					ownerBoardJPanel.repaint();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		});
		animatorThread.start();
	}
	
	public Thread getAnimatorThread(){
		return animatorThread;
	}
	
	public ChessPiece getPieceValue(){
		return pieceValue;
	}
	
	public int getCurrentX() {
		return currentX;
	}

	public int getCurrentY() {
		return currentY;
	}

	public BoardCell getStartCell() {
		return startCell;
	}

	private BoardJPanel ownerBoardJPanel;
	private ChessPiece pieceValue;
	private Thread animatorThread;
	private int currentX, currentY;
	private int finalX, finalY;
	private BoardCell startCell;
}
